﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TheStar.Lib
{
    public class Path : ICloneable, IComparable, IComparable<Path>
    {
        #region fields
        private readonly ArrayList paths;
        private double cost; 
        #endregion

        #region constructors
        private Path()
        {
            this.paths = new ArrayList();
        }
        public Path(object node)
            : this()
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            this.paths.Add(node);
        }
        protected Path(Path pathItem)
            : this()
        {
            foreach (object node in pathItem.paths)
            {
                this.paths.Add(node);
            }
            this.cost = pathItem.cost;
        }
        #endregion

        public virtual double Cost
        {
            get { return cost; }
        }
        public object Last
        {
            get
            {
                ArrayList list = this.paths;
                return list[list.Count - 1];
            }
        }
        public virtual void Add(object node, double cost)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (cost == 0)
            {
                throw new ArgumentOutOfRangeException("cost");
            }
            this.paths.Add(node);
            this.cost += cost;
        }

        public virtual bool HasNode(object node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            return this.paths.Contains(node);
        }
        public virtual object Clone()
        {
            return new Path(this);
        }
        public virtual int CompareTo(object obj)
        {
            return this.CompareTo(obj as Path);
        }
        public virtual int CompareTo(Path other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            else
            {
                return this.Cost.CompareTo(other.Cost);
            }
        }
    }
}
